package fag.core.game.test.playground.doodad;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import fag.core.game.test.playground.AbstractPlaygroundDoodad;
import fag.core.geometry.Rectangle2D;

/**
 * Barre de chargement.
 * @author lumz
 */
public class LoadingBar extends AbstractPlaygroundDoodad {

	private static final Dimension size = new Dimension(200, 20);
	
	private static final int SPANNING = 2;
	
	private static final Color BACKGROUND_COLOR = Color.WHITE;
	private static final Color FOREGROUND_COLOR = Color.LIGHT_GRAY;
	
	private int progress = 0;
	
	/**
	 * Constructeur par défaut.
	 */
	public LoadingBar() {
		super(new Rectangle2D(-size.getWidth() / 2, -size.getHeight() /2, size.getWidth(), size.getHeight()));
	}
	
	/**
	 * Fixer la valeur de progression.
	 * @param progress La nouvelle valeur de progression.
	 */
	public void setProgress(int progress) {
		
		this.progress = progress;

		scene.addDirtyRegion(getBounds());
	}

	@Override
	protected void draw(Graphics2D graphics2d) {
		
		// Transformation du plan pour peindre l'objet à sa position
		AffineTransform old = graphics2d.getTransform();
		
		synchronized(transformation) {
			graphics2d.transform(transformation);
		}
		
		graphics2d.setColor(BACKGROUND_COLOR);
		graphics2d.fillRect((int)-size.getWidth() / 2, (int)-size.getHeight() / 2, (int)size.getWidth() + 1, (int)size.getHeight() + 1);
		graphics2d.setColor(FOREGROUND_COLOR);
		graphics2d.fillRect((int)-size.getWidth() / 2 + SPANNING, (int)-size.getHeight() / 2 + SPANNING, (int)size.getWidth() * progress / 100 - SPANNING * 2 + 1, (int)size.getHeight() - 2 * SPANNING + 1);
		
		// Restauration du contexte graphique
		graphics2d.setTransform(old);
	}
}
